package com.gdiss.www.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * SpringSecurity的配置类
 * @author Flandre
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.formLogin()                            //自定义编写的登陆页面
                //.loginPage("/login")                      //登陆页面设置
                //.loginProcessingUrl("/login")             //登录访问的路径
                .defaultSuccessUrl("/test/index").permitAll()  //登录成功后跳转的路径
                .and().authorizeRequests()
                .antMatchers("/").permitAll()   //设置不需要认证的访问路径，因为暂时还没开发完所以默认所有路径都能访问
                .anyRequest().authenticated()
                .and().csrf().disable();

    }
}
